﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CL3_IF_DllSample;
using LogicBase;
using System.Runtime.InteropServices;

namespace CL_Native
{
    public class CL_GetValue:Logic
    {

        private const int MaxRequestDataLength = 512000;
        [OutPut ,GroupBox("Output")]
        public lgcExpNumber OUT1_Value { get; set; } = new lgcExpNumber();
        [OutPut, GroupBox("Output")]
        public lgcExpNumber OUT2_Value { get; set; } = new lgcExpNumber();
        [OutPut, GroupBox("Output")]
        public lgcExpNumber OUT3_Value { get; set; } = new lgcExpNumber();
        [OutPut, GroupBox("Output")]
        public lgcExpNumber OUT4_Value { get; set; } = new lgcExpNumber();
        [OutPut, GroupBox("Output")]
        public lgcExpNumber OUT5_Value { get; set; } = new lgcExpNumber();
        [OutPut, GroupBox("Output")]
        public lgcExpNumber OUT6_Value { get; set; } = new lgcExpNumber();
        [OutPut, GroupBox("Output")]
        public lgcExpNumber OUT7_Value { get; set; } = new lgcExpNumber();
        [OutPut, GroupBox("Output")]
        public lgcExpNumber OUT8_Value { get; set; } = new lgcExpNumber();
        public CL_GetValue()
        {

        }
        public override Result ToolDo(RunNode runNode, RunControl runControl)
        {

            byte[] buffer = new byte[MaxRequestDataLength];
            using (PinnedObject pin = new PinnedObject(buffer))
            {
                CL3IF_MEASUREMENT_DATA measurementData = new CL3IF_MEASUREMENT_DATA();
                measurementData.outMeasurementData = new CL3IF_OUTMEASUREMENT_DATA[NativeMethods.CL3IF_MAX_OUT_COUNT];

                int returnCode = NativeMethods.CL3IF_GetMeasurementData(0, pin.Pointer);
                if (returnCode != NativeMethods.CL3IF_RC_OK)
                {
                    ShowLog.Error("CL3IF_GetMeasurementData error");
                    return Result.ERR;
                }

                measurementData.addInfo = (CL3IF_ADD_INFO)Marshal.PtrToStructure(pin.Pointer, typeof(CL3IF_ADD_INFO));
                int readPosition = Marshal.SizeOf(typeof(CL3IF_ADD_INFO));
                for (int i = 0; i < NativeMethods.CL3IF_MAX_OUT_COUNT; i++)
                {
                    measurementData.outMeasurementData[i] = (CL3IF_OUTMEASUREMENT_DATA)Marshal.PtrToStructure(pin.Pointer + readPosition, typeof(CL3IF_OUTMEASUREMENT_DATA));
                    readPosition += Marshal.SizeOf(typeof(CL3IF_OUTMEASUREMENT_DATA));


                }

                OUT1_Value.setDouble(measurementData.outMeasurementData[0].measurementValue/10000);
                OUT2_Value.setDouble(measurementData.outMeasurementData[1].measurementValue/10000);
                OUT3_Value.setDouble(measurementData.outMeasurementData[2].measurementValue/10000);
                OUT4_Value.setDouble(measurementData.outMeasurementData[3].measurementValue/10000);
                OUT5_Value.setDouble(measurementData.outMeasurementData[4].measurementValue/10000);
                OUT6_Value.setDouble(measurementData.outMeasurementData[5].measurementValue/10000);
                OUT7_Value.setDouble(measurementData.outMeasurementData[6].measurementValue/10000);
                OUT8_Value.setDouble(measurementData.outMeasurementData[7].measurementValue/10000);
            }
            return Result.OK;
        }
    }
}
